package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Catalog;
import com.itheima.mapper.store.CatalogMapper;
import com.itheima.service.CatalogService;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.UidUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class CatalogServiceImpl implements CatalogService {
    //查找所有
    @Override
    public List<Catalog> findAll(String pageNum, String pageSize) {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getSession();
        //获取mapper对象
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);
        //默认当前页为1
        int pageN=1;
        //默认每页显示条目数为10
        int pageS=10;
        //对获取的当前页码和每页显示条目数进来类型处理
        if(StringUtils.isNotBlank(pageNum)){
            pageN=Integer.parseInt(pageNum);
        }
        if(StringUtils.isNotBlank(pageSize)){
            pageS=Integer.parseInt(pageSize);
        }
        //使用分页插件设置分页参数
        PageHelper.startPage(pageN,pageS);

        //调用方法
        List<Catalog> list = mapper.findAll();
        //关闭资源
        MapperUtil.close(session);
        return list;
    }

    //新建
    @Override
    public void save(Catalog catalog) {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getSession();
        //获取mapper对象
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);

        //主键id处理
        catalog.setId(UidUtil.getUid());

        //创建时间处理
        catalog.setCreateTime(new Date());
        mapper.save(catalog);
        //3、关闭资源
        MapperUtil.close(session);
    }

    //根据id删除
    @Override
    public Boolean deleteByIds(String ids) {
        SqlSession session=null;
        boolean flag=false;//删除失败
        try {
            //1、获取sesssion
            session = MapperUtil.getSession();
            //2、获取代理对象
            CatalogMapper mapper = session.getMapper(CatalogMapper.class);
            String[] id = ids.split(",");//["1","2","3"]
            mapper.deleteByIds(id);
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //3、关闭资源
            MapperUtil.close(session);
        }

        return flag;
    }

    //根据id查找
    @Override
    public Catalog findById(String id) {
        //1、获取sesssion
        SqlSession session = MapperUtil.getSession();
        //2、获取代理对象
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);
        Catalog catalog=mapper.findById(id);
        //3、关闭资源
        MapperUtil.close(session);
        return catalog;
    }

    //更新数据
    @Override
    public void update(Catalog catalog) {
        //1、获取sesssion
        SqlSession session = MapperUtil.getSession();
        //2、获取代理对象
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);

        mapper.update(catalog);

        //3、关闭资源
        MapperUtil.close(session);
    }

    @Override
    public List<Catalog> list() {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getSession();
        //获取mapper对象
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);
        //调用方法
        List<Catalog> list = mapper.findAll();
        //关闭资源
        MapperUtil.close(session);
        return list;
    }
}
